﻿using Abp.Collections.Extensions;
using FutureApp.Authorization;
using FutureApp.EntityFramework;
using FutureApp.Navigation;
using System.Collections.Generic;
using System.Linq;

namespace FutureApp.Migrations.SeedData
{
    public class DefaultMenusCreator
    {
        public static List<Menu> InitialMenus { get; private set; }

        static DefaultMenusCreator()
        {
            InitialMenus = new List<Menu>()
            {
                new Menu
                {
                    Code = "System",
                    Name = "SystemAdministration",
                    Icon = "fa fa-cog",
                    RequiresAuthentication = true,
                    Children = new List<Menu>
                    {
                        new Menu
                        {
                            Code = "Users",
                            Name = "UsersAdministration",
                            NavigationUrl = "Users/List",
                            Icon = "fa fa-users",
                            RequiredPermission = PermissionNames.Pages_Users,
                            Order = 1
                        },
                        new Menu
                        {
                            Code = "Roles",
                            Name = "RolesAdministration",
                            NavigationUrl = "Roles/List",
                            Icon = "fa fa-male",
                            RequiredPermission = PermissionNames.Pages_Roles,
                            Order = 2
                        },
                        new Menu
                        {
                            Code = "Permissions",
                            Name = "PermissionsDefinition",
                            NavigationUrl = "Permissions/List",
                            Icon = "fa fa-male",
                            RequiredPermission = PermissionNames.Pages_Permissions,
                            Order = 3
                        },
                        new Menu
                        {
                            Code = "Tenants",
                            Name = "TenantsAdministration",
                            NavigationUrl = "Tenants/List",
                            Icon = "fa fa-globe",
                            RequiredPermission = PermissionNames.Pages_Tenants,
                            Order = 4
                        },
                        new Menu
                        {
                            Code = "About",
                            Name = "About",
                            NavigationUrl = "About",
                            Icon = "fa fa-info",
                            Order = 5
                        }
                    },
                    Order = 1
                },
                new Menu
                {
                    Code = "Goods",
                    Name = "GoodsAdministration",
                    Icon = "fa fa-gift",
                    RequiresAuthentication = true,
                    Children = new List<Menu>
                    {
                        new Menu
                        {
                            Code = "GoodsInfo",
                            Name = "GoodsInfomation",
                            NavigationUrl = "Goods/List",
                            RequiredPermission = PermissionNames.Pages_Goods,
                            Icon = "fa fa-info-circle",
                            Order = 1
                        },
                        new Menu
                        {
                            Code = "GoodsMould",
                            Name = "GoodsMould",
                            NavigationUrl = "GoodsMoulds/List",
                            RequiredPermission = PermissionNames.Pages_Goods,
                            Icon = "fa fa-cubes",
                            Order = 2
                        },
                        new Menu
                        {
                            Code = "GoodsAttribute",
                            Name = "GoodsAttribute",
                            NavigationUrl = "GoodsAttributes/List",
                            RequiredPermission = PermissionNames.Pages_Goods,
                            Icon = "fa fa-fire",
                            Order = 3
                        },
                        new Menu
                        {
                            Code = "GoodsCategory",
                            Name = "GoodsCategory",
                            NavigationUrl = "GoodsCategories/List",
                            RequiredPermission = PermissionNames.Pages_Goods,
                            Icon = "fa fa-bookmark",
                            Order = 4
                        },
                        new Menu
                        {
                            Code = "GoodsProperty",
                            Name = "GoodsProperty",
                            NavigationUrl = "GoodsProperties/List",
                            RequiredPermission = PermissionNames.Pages_Goods,
                            Icon = "fa fa-flash",
                            Order = 5
                        },
                        new Menu
                        {
                            Code = "GoodsBrand",
                            Name = "GoodsBrand",
                            NavigationUrl = "GoodsBrands/List",
                            RequiredPermission = PermissionNames.Pages_Goods,
                            Icon = "fa fa-star",
                            Order = 6
                        },
                    },
                    Order = 2
                },
                new Menu
                {
                    Code = "Develop",
                    Name = "Develop",
                    Icon = "fa fa-code",
                    RequiresAuthentication = true,
                    Order = 1000
                }
            };
        }

        private readonly FutureAppDbContext _context;

        public DefaultMenusCreator(FutureAppDbContext context)
        {
            this._context = context;
        }

        public void Create()
        {
            InitialMenus.ForEach(this.AddIfNotExists);
            this._context.SaveChanges();
        }

        private void AddIfNotExists(Menu menu)
        {
            if (!this._context.Menus.Any(s => s.Code == menu.Code))
            {
                this._context.Menus.Add(menu);
            }
            if (!menu.Children.IsNullOrEmpty())
            {
                menu.Children.ToList().ForEach(m =>
                {
                    var existMenu = this._context.Menus.FirstOrDefault(s => s.Code == menu.Code);
                    m.ParentId = (existMenu ?? menu).Id;
                    this.AddIfNotExists(m);
                });
            }
        }
    }
}
